var pu = require("../tool/public")
var mg = require("../db/userdb")
var redis = require("../db/redis")


class role {

    /*角色绑定用户*/
    async role_join_users(req, res, next) {
        let team_userid = req.body.team_userid
        let roleid = req.body.roleid

        let isnull = pu.isnull([roleid, team_userid])
        if (isnull) {
            pu.fail("参数不正确", res)
            return
        }
        if (typeof roleid !== "object") {
            pu.fail("角色id必须是数组", res)
            return
        }

        var role = []
        for (var i = 0; i < roleid.length; i++) {
            role.push(mg.ObjectID(roleid[i]))
        }
        await mg.updateOne("team_users", {_id: mg.ObjectID(team_userid)}, {$set: {role: role}})
        await redis.set("USER_ROLE_" + team_userid, roleid.toString())
        pu.success("OK", "", res)
        return

    }

    /*角色绑定角色组*/
    async role_join_group(req, res, next) {
        var group_id = req.body.group_id
        var role_id = req.body.role_id
        let isnull = pu.isnull([role_id, group_id])
        if (isnull) {
            pu.fail("参数不正确", res)
            return
        }

        let joinfo = mg.findOne("role", {_id: mg.ObjectID(role_id)})
        if (joinfo.group_id) { //该角色已经有分组
            pu.fail("该角色已经有分组", res)
            return
        } else {
            await mg.updateOne("role", {_id: mg.ObjectID(role_id)}, {$set: {group_id: mg.ObjectID(group_id)}})
            pu.success("OK", "", res)
            return
        }

    }

    /*删除角色*/
    async delete_role(req, res, next) {

        var role_idlist = req.body.role_idlist
        var team_id = req.headers.team
        let isnull = pu.isnull([role_idlist])
        if (isnull) {
            pu.fail("参数不正确", res)
            return
        }

        var where = {team_id: mg.ObjectID(team_id)}

        where["$or"] = []
        for (var i = 0; i < role_idlist.length; i++) {
            where["$or"].push({_id: mg.ObjectID(role_idlist[i])})
        }


        let list = await mg.find("role", where)
        var is_delete = true
        for (var i = 0; i < list.length; i++) {
            if (list[i].jurlist && list[i].length) {
                is_delete = false
            }
        }
        if (!is_delete) {
            pu.fail("角色下还有权限未解除", res)
            return
        } else {
            await mg.deleteAll("role", where)
            for (var i = 0; i < role_idlist.length; i++) {
                await redis.hdel("JURINFO_ROLE_" + team_id, role_idlist[i])
            }
            pu.success("OK", "", res)
        }
    }

    /*新增角色*/
    async add_role(req, res, next) {
        var name = req.body.name
        var team_id = req.headers.team
        var group_id = req.body.group_id
        let isnull = pu.isnull([name])
        if (isnull) {
            pu.fail("参数不正确", res)
            return
        }
        var isnert = {name: name, time: +new Date(), team_id: mg.ObjectID(team_id)}

        if (group_id) {
            isnert.group_id = mg.ObjectID(group_id)
        }
        let inserinfo = await mg.insertOne("role", isnert)
        isnert._id = inserinfo.insertedId
        pu.success("OK", isnert, res)
        return
    }

    /*修改角色组*/
    async update_role_group(req, res, next) {
        var name = req.body.name
        var _id = req.body._id

        let isnull = pu.isnull([name, _id])
        if (isnull) {
            pu.fail("参数不正确", res)
            return
        }
        await mg.updateOne("role_group", {_id: mg.ObjectID(_id)}, {$set: {name: name}})
        pu.success("OK", "", res)
        return
    }

    /*查询*/
    async find_role_group(req, res, next) {
        console.log(req.body)
        var team_id = req.headers.team
        var _id = req.body._id

        if (_id) {
            let info = await mg.findOne("role_group", {_id: mg.ObjectID(_id)})
            pu.success("OK", info, res)
            return
        }
        let info = await mg.find("role_group", {team_id: mg.ObjectID(team_id)})
        pu.success("OK", info, res)
        return
    }

    /*新增角色组*/
    async add_role_group(req, res, next) {
        var name = req.body.name
        var group_id = req.body.group_id
        var team_id = req.headers.team
        let isnull = pu.isnull([name])
        if (isnull) {
            pu.fail("参数不正确", res)
            return
        }
        var isnert = {name: name, time: +new Date(), team_id: mg.ObjectID(team_id), group_id: mg.ObjectID(group_id)}
        let inserinfo = await mg.insertOne("role_group", isnert)
        isnert._id = inserinfo.insertedId
        pu.success("OK", isnert, res)
        return
    }

    /*修改角色组*/
    async update_role(req, res, next) {
        var name = req.body.name
        var _id = req.body._id

        let isnull = pu.isnull([name, _id])
        if (isnull) {
            pu.fail("参数不正确", res)
            return
        }
        await mg.updateOne("role", {_id: mg.ObjectID(_id)}, {$set: {name: name}})
        pu.success("OK", "", res)
        return
    }

    /*删除角色组*/
    async delete_role_group(req, res, next) {
        var _id = req.body._id
        var team_id = req.headers.team
        let isnull = pu.isnull([_id])
        if (isnull) {
            pu.fail("参数不正确", res)
            return
        }
        let role = await mg.findOne("role", {team_id: mg.ObjectID(team_id), group_id: _id})
        if (role) {
            pu.fail("该分组下面还有其他角色", res)
            return
        } else {
            mg.deleteOne("role_group", {_id: mg.ObjectID(_id)})
            pu.success("OK", {}, res)
        }
    }

    /*查询角色*/
    async find_role(req, res, next) {
        var team_id = req.headers.team
        var _id = req.body._id

        var text = req.body.text
        var group_id = req.body.group_id
        if (_id) {
            let info = await mg.findOne("role", {_id: mg.ObjectID(_id)})
            pu.success("OK", info, res)
            return
        }
        var where = {team_id: mg.ObjectID(team_id)}
        if (group_id) {
            where.group_id = mg.ObjectID(group_id)
        }
        if (text) {
            where.name = {$regex: text}
        }
        let info = await mg.find("role", where)
        pu.success("OK", info, res)
        return
    }

}


module.exports = new role()